<template>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="username">
            <el-input type="username" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>

    export default {
        data() {
            var username = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入账号'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            return {
                ruleForm: {
                    password: 'tiger',
                    username: 'admin',
                },
                rules: {
                  password: [
                        {validator: validatePass, trigger: 'blur'},
                    ],
                  username: [
                        {validator: username, trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
              this.$refs[formName].validate((valid) => {
                if (valid) {
                  this.$http({
                    method: 'get',
                    url: 'sso/userLogin/auth?userName='+this.ruleForm.username+'&password='+this.ruleForm.password
                  }).then((r)=>{
                    console.log(r)
                    if (r.data.returnCode!=null&&r.data.returnCode==2000){
                      //登录成功，将信息保存
                       sessionStorage.setItem("accessToken",r.data.t.accessToken);
                       this.$router.push("/index");
                    } else {
                      alert("用户名或密码错误！");
                    }
                  });
                } else {
                  alert('error submit!!');
                  return false;
                }
              });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        },
    }
</script>

<style scoped>
    .el-form {
        width: 400px;
        margin: auto;
        margin-top: 10%;
    }
</style>
